package com.glory.dgcpcpda.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.SettingActivity;
import com.glory.dgcpcpda.base.Constant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pda.platform.ui.ui_pdaplatform.adapter.FreeUI_TabIndexGridAdapter;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.glory.dgcpcpda.utils.MyUtils.setPicToDrawable;

/**
 * FileName: MainFragment
 * Author: XJH
 * Date: 2020/5/13 14:40
 * Description:主界面
 * History:
 */
public class MainFragment extends FreeUI_BaseFragment {
    @BindView(R.id.tvTtitle)
    TextView tvTitle;
    @BindView(R.id.ivSetting)
    ImageView ivSetting;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rvTechnicalServiceList)
    RecyclerView rvTechnicalServiceList;
    @BindView(R.id.rvCheckList)
    RecyclerView rvCheckList;
    @BindView(R.id.rvRecipeList)
    RecyclerView rvRecipeList;
    @BindView(R.id.rvMFGList)
    RecyclerView rvMFGList;
    @BindView(R.id.rvCPCList)
    RecyclerView rvCPCList;
    @BindView(R.id.llTechnicalServiceLayout)
    LinearLayout llTechnicalServiceLayout;
    @BindView(R.id.llCheckLayout)
    LinearLayout llCheckLayout;
    @BindView(R.id.llRecipeLayout)
    LinearLayout llRecipeLayout;
    @BindView(R.id.llMFGLayout)
    LinearLayout llMFGLayout;
    @BindView(R.id.llCPCLayout)
    LinearLayout llCPCLayout;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initView() {
        tvTitle.setText("主界面");
        rvTechnicalServiceList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvCheckList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvRecipeList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvMFGList.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rvCPCList.setLayoutManager(new GridLayoutManager(getActivity(),3));
    }

    @Override
    protected void initData() {
        try {
            initTechnicalServiceArea();
            initCheckArea();
            initRecipeArea();
            initMFGArea();
            initCPCArea();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initRecipeArea() throws IOException {
        InputStream recipeStream = getActivity().getApplicationContext().getResources().getAssets().open("recipe.txt");
        String recipeJson = FreeApi_StringUtils.getString(recipeStream);
        List<FreeUI_TabIndexGridEntity> recipeEntity = filteringData(new Gson().fromJson(recipeJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (recipeEntity.size() <= 0){
            llRecipeLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter recipeGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), recipeEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        recipeGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recipeGridAdapter.isFirstOnly(true);
        rvRecipeList.setAdapter(recipeGridAdapter);
    }

    private void initCheckArea() throws IOException {
        InputStream checkJsonStream = getActivity().getApplicationContext().getResources().getAssets().open("check.txt");
        String checkJson = FreeApi_StringUtils.getString(checkJsonStream);
        List<FreeUI_TabIndexGridEntity> checkEntity = filteringData(new Gson().fromJson(checkJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (checkEntity.size() <= 0){
            llCheckLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter checkGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), checkEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        checkGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        checkGridAdapter.isFirstOnly(true);
        rvCheckList.setAdapter(checkGridAdapter);
    }

    private void initTechnicalServiceArea() throws IOException {
        InputStream technicalServiceStream = getActivity().getApplicationContext().getResources().getAssets().open("technicalService.txt");
        String technicalService = FreeApi_StringUtils.getString(technicalServiceStream);
        List<FreeUI_TabIndexGridEntity> technicalServiceEntity = filteringData(new Gson().fromJson(technicalService, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (technicalServiceEntity.size() <= 0){
            llTechnicalServiceLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter technicalServiceGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), technicalServiceEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        technicalServiceGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        technicalServiceGridAdapter.isFirstOnly(true);
        rvTechnicalServiceList.setAdapter(technicalServiceGridAdapter);
    }

    private void initMFGArea() throws IOException {
        InputStream mfgJsonStream = getActivity().getApplicationContext().getResources().getAssets().open("mfg.txt");
        String mfgJson = FreeApi_StringUtils.getString(mfgJsonStream);
        List<FreeUI_TabIndexGridEntity> mfgEntity = filteringData(new Gson().fromJson(mfgJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (mfgEntity.size() <= 0){
            llMFGLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter mfgGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), mfgEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        mfgGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mfgGridAdapter.isFirstOnly(true);
        rvMFGList.setAdapter(mfgGridAdapter);
    }

    private void initCPCArea() throws IOException {
        InputStream cpcJsonStream = getActivity().getApplicationContext().getResources().getAssets().open("cpc.txt");
        String cpcJson = FreeApi_StringUtils.getString(cpcJsonStream);
        List<FreeUI_TabIndexGridEntity> cpcEntity = filteringData(new Gson().fromJson(cpcJson, new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
        }.getType()));
        if (cpcEntity.size() <= 0){
            llCPCLayout.setVisibility(View.GONE);
            return;
        }
        FreeUI_TabIndexGridAdapter cpcGridAdapter = new FreeUI_TabIndexGridAdapter(setPicToDrawable(getActivity(), cpcEntity), R.layout.free_ui_glory_adapter_tab_index_grid);
        cpcGridAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        cpcGridAdapter.isFirstOnly(true);
        rvCPCList.setAdapter(cpcGridAdapter);
    }

    private List<FreeUI_TabIndexGridEntity> filteringData(List<FreeUI_TabIndexGridEntity> entity) {
        Iterator it = entity.iterator();
        while (it.hasNext()) {
            FreeUI_TabIndexGridEntity item = (FreeUI_TabIndexGridEntity) it.next();
            try {
                Class<?> aClass = Class.forName(item.getSkipClass());
                String name = aClass.getSimpleName();
                if (!Constant.AUTHORITY_LIST.contains(name)) {

                    it.remove();

                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return entity;
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @OnClick(R.id.ivSetting)
    public void onViewClicked() {
        FreeApi_Utils.skipWithExtraIntent("header", getActivity(), SettingActivity.class);
    }
}
